package com.baizhi.controller;


import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;


    //删除
    @GetMapping("/delete")
    public ResponseEntity<String> removeUser(Integer id){
        System.out.println("id==============:"+id);
        Integer row = userService.removeUserById(id);
        if(row==1){
            return new ResponseEntity<>("成功",HttpStatusCode.valueOf(200));
        }else{
            return new ResponseEntity<>("失败",HttpStatusCode.valueOf(500));
        }
    }


    //修改
    @PostMapping("/update")
    public ResponseEntity<String> updateUser(User user){
        Integer row = userService.updateUser(user);
        if(row==1){
            return new ResponseEntity<>("成功",HttpStatusCode.valueOf(200));
        }else{
            return new ResponseEntity<>("失败",HttpStatusCode.valueOf(500));
        }
    }


    @PostMapping("/add")
    public ResponseEntity<String> registUser(User user){
        System.out.println("=========================================="+user);
        Integer row = userService.addUser(user);
        if(row==1){
            System.out.println("=======================userId="+user.getId());
            return new ResponseEntity<String>("成功", HttpStatusCode.valueOf(200));

        }else{
            return new ResponseEntity<>("添加失败",HttpStatusCode.valueOf(500));
        }

    }


    @GetMapping("/all")
    public List<User> getUsers(){
        return userService.findAllUsers();
    }
}
